<?php
/*
Extension Name: Hidden Text 2
Extension Url: http://lussumo.com/addons/
Description: Allows users to hide part of a discussion
Version: 2.2
Author: Justin (Krak) Haury
Author Url: http://mymindisgoing.org

*/


if (!defined('IN_VANILLA')) exit();

global $Context, $Configuration, $NoticeCollector, $Page;

// Language dictionary
$Context->SetDefinition('ExtensionOptions', 'Options');
$Context->SetDefinition('HiddenTextSettings', 'Change Hidden Text Marker');
$Context->SetDefinition('HiddenTextMarkerShow', 'Change The Show Hidden Text Marker');
$Context->SetDefinition('HiddenTextMarkerHide', 'Change The Hide Hidden Text Marker');
$Context->SetDefinition('HiddenTextSettingsTitle', 'Hidden Text Settings');
$Context->SetDefinition('HiddenTextSettingsInfoShow',
	'Enter the text you wish to appear for the link that will show the hidden text. '
	. 'This link is located at the beginning of the hidden text blurb. Default is (show text)');
$Context->SetDefinition('HiddenTextSettingsInfoHide',
	'Enter the text you wish to appear for the link to hide the hidden text '
	.'once it has been made visible. This link is at the end of the hidden text blurb. '
	.'Default is (hide text)');
$Context->SetDefinition('SettingsInputTextShow', 'Show Hidden Text Marker');
$Context->SetDefinition('SettingsInputTextHide', 'Hide Hidden Text Marker');
$Context->SetDefinition('ErrCreateConfig',
	'Could not save Hidden Text marker settings to configuration file!');
$Context->SetDefinition('PERMISSION_HIDDEN_TEXT', 'Can see and use hidden text feature');
$Context->SetDefinition('InsertHiddenText', 'insert hidden text');
$Context->SetDefinition('HiddenTextRemoved', '');

// Default permissions
$Context->Configuration['PERMISSION_HIDDEN_TEXT'] = '0';

// Settings
if (!array_key_exists('HIDDEN_TEXT_SETUP', $Configuration)
	|| 2 > (int) $Configuration['HIDDEN_TEXT_SETUP']
) {
	AddConfigurationSetting($Context, 'HIDDEN_TEXT_MARKER_SHOW', '(show)');
	AddConfigurationSetting($Context, 'HIDDEN_TEXT_MARKER_HIDE', '(hide)');
	AddConfigurationSetting($Context, 'HIDDEN_TEXT_USE_JQUERY', '0');
	AddConfigurationSetting($Context, 'HIDDEN_TEXT_SETUP', 2);

	if (!$Context->WarningCollector->Iif() && $NoticeCollector) {
		// Could not save configuration
		$NoticeCollector->AddNotice($Context->GetDefinition('ErrCreateConfig'));
	}
}

// If you are on the settings page and the permission to hide/show hidden text
// Add the HiddenText setting page.
if ($Context->SelfUrl == "settings.php"
	&& $Context->Session->User->Permission('PERMISSION_HIDDEN_TEXT')
) {

	include 'library/Control.HiddenTextForm.php';

	$HiddenTextForm = $Context->ObjectFactory->NewContextObject($Context, 'HiddenTextForm');
	$Page->AddRenderControl($HiddenTextForm, $Configuration["CONTROL_POSITION_BODY_ITEM"] + 1);

	$ExtensionOptions = $Context->GetDefinition("ExtensionOptions");
	$Panel->AddList($ExtensionOptions, 20);
	$Panel->AddListItem($ExtensionOptions, $Context->GetDefinition("HiddenTextSettings"),
		GetUrl($Context->Configuration, 'settings.php', '', '', '', '', 'PostBackAction=HiddenText'));

}

// Add a preference to the users account page - they can decide whether or not to use the extension
// - on by default
if ($Context->Session->User->Permission('PERMISSION_HIDDEN_TEXT')
	&& $Context->SelfUrl == 'account.php'
	&& $Context->Session->UserID > 0
) {

	$PostBackAction = ForceIncomingString('PostBackAction', '');
	if ($PostBackAction == 'Functionality') {

		function PreferencesForm_AddHiddenTextPreference(&$PreferencesForm) {
			$PreferencesForm->AddPreference(
				'Hidden Text', 'Disable the hidden text extension', 'HideHiddenText', 1);
		}
		$Context->AddToDelegate(
			'PreferencesForm', 'Constructor', 'PreferencesForm_AddHiddenTextPreference');
	}
}


// Hide hidden text.
$HiddenTextCanBeShown = 0;

if ($Context->Session->User->Permission('PERMISSION_HIDDEN_TEXT')
	&& !$Context->Session->User->Preference('HideHiddenText')
	&& in_array($Context->SelfUrl, array("post.php", "comments.php"))
) {

	// Add assets
	$Head->AddStyleSheet("extensions/HiddenText/style.css");

	// Can use jQuery or Moo-tool library
	if ($Configuration['HIDDEN_TEXT_USE_JQUERY']) {
		$Head->AddScript('js/jquery.js');

		// if LowCal-vanilla is installed, packed our js with the rest.
		if (!empty($Configuration['LOWCALVANILLA_TOOLS_PATH'])) {
			require_once($Configuration['LOWCALVANILLA_TOOLS_PATH']);
			LowCalVanilla_AddScript($Context, $Head, 'extensions/HiddenText/jq-hide.js');
		} else {
			$Head->AddScript('extensions/HiddenText/jq-hide.js');
		}
	} else {
		if (!function_exists('MT_AddComponent')) {
			$Head->AddScript('extensions/HiddenText/moo-hide.js');
		} else {
			MT_AddComponent('Fx.Slide', 'Window.DomReady', 'Element.Selectors');
		}
		$Head->AddScript('extensions/HiddenText/hide.js');
	}


	// Add the link to insert the code brackets
	function Comment_HiddenText(&$CommentGrid) {
		$CommentList = &$CommentGrid->DelegateParameters["CommentList"];
		$CommentList .=	'<a href="#" '
			.'onclick="HiddenText(document.getElementById(\'CommentBox\'),\'\'); return false;">'
			. $CommentGrid->Context->GetDefinition('InsertHiddenText') .'</a>';
		echo $CommentList;
	}

	if ($Context->Session->UserID > 0) {
		// Adds the link next to the big input button on the start new discussion page
		$Context->AddToDelegate(
			'DiscussionForm', 'DiscussionForm_PostCommentToggle', 'Comment_HiddenText');

		// Adds the link next to the big input button for a comment on a discussion page
		$Context->AddToDelegate(
			'DiscussionForm', 'CommentForm_PostCommentToggle', 'Comment_HiddenText');
	}

	$HiddenTextCanBeShown = 1;

}

include 'library/HiddenTextParser.php';
$HiddenText = $Context->ObjectFactory->NewContextObject($Context, 'HiddenTextParser', $HiddenTextCanBeShown);
$Context->StringManipulator->AddGlobalManipulator("HiddenText", $HiddenText);

